Imports System.IO
Imports System.Xml.Serialization
Imports System.Runtime.CompilerServices
Imports System.Xml
Imports System.ComponentModel

Public Module XmlSerizationExtentions

    Public Function IsInDesignMode() As Boolean
        Return DesignerProperties.GetIsInDesignMode(New DependencyObject)
    End Function

    <Extension()>
    Public Function DeserializeFromXMLFile(Of T As {Class, New})(ByVal fullFileName As String) As T
        If My.Computer.FileSystem.FileExists(fullFileName) Then
            Try

                Dim xmlDoc As New XmlDocument
                xmlDoc.Load(fullFileName)
                Try
                    Return DeserializeXML(Of T)(xmlDoc)
                Catch ex As Exception


                End Try
            Catch ex As Exception

            End Try
        End If
        Return New T
    End Function
    <Extension()>
    Public Function DeserializeXML(Of T As {Class, New})(ByVal xml As String) As T
        Try
            Using reader As TextReader = New StringReader(xml)
                Return DirectCast(New XmlSerializer(GetType(T)).Deserialize(reader), T)
            End Using
        Catch ex As Exception

        End Try
        Return New T
    End Function
    <Extension()>
    Public Function DeserializeXML(Of T As {Class, New})(ByVal doc As XmlDocument) As T
        Try
            Using reader As TextReader = New StringReader(doc.DocumentElement.OuterXml)
                Dim rootCollection As T = DirectCast(New XmlSerializer(GetType(T)).Deserialize(reader), T)
                Return rootCollection
            End Using
        Catch ex As Exception

        End Try
        Return New T
    End Function
    <Extension()>
    Public Function DeserializeXML(Of T As {Class, New})(ByVal doc As XmlNode) As T
        Try
            Dim rootCollection As T = Nothing
            If (doc IsNot Nothing) Then
                Using reader As TextReader = New StringReader(doc.OuterXml)
                    rootCollection = DirectCast(New XmlSerializer(GetType(T)).Deserialize(reader), T)

                End Using
            End If
            Return rootCollection
        Catch ex As Exception

        End Try
        Return New T
    End Function
    <Extension()>
    Public Function SerializeXML(Of T As {Class, New})(ByVal rootClass As T) As XmlDocument
        Dim xml As String = SerializeToXMLString(rootClass)
        Dim doc As New XmlDocument() With {.PreserveWhitespace = True}
        doc.LoadXml(xml)
        Return doc
    End Function
    <Extension()>
    Public Function SerializeToXMLString(Of T As {Class, New})(ByVal rootClass As T) As String
        Dim xml As String = ""
        Using w As TextWriter = New StringWriter()
            Dim objtype As Type = GetType(T)
            Dim serialer As XmlSerializer = New XmlSerializer(objtype)
            serialer.Serialize(w, rootClass)
            w.Flush()
            xml = w.ToString()
        End Using

        Return xml.Trim().Replace("xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""", "").Replace("utf-16", "utf-8")
    End Function
    <Extension()>
    Public Function SerializeToBasicXMLString(Of T As {Class, New})(ByVal rootClass As T) As String
        Dim xml As String = ""
        Using w As TextWriter = New StringWriter()
            Dim objtype As Type = GetType(T)
            Dim serialer As XmlSerializer = New XmlSerializer(objtype)
            serialer.Serialize(w, rootClass)
            w.Flush()
            xml = w.ToString()
        End Using
        If Not String.IsNullOrWhiteSpace(xml) Then
            Dim xmlstring As String = xml.Replace("<?xml version=""1.0"" encoding=""utf-16""?>", "").Replace(" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""", "").Trim()
            Return xmlstring
        End If
        Return ""
    End Function

End Module

